<?php

class Tourismgoodsexotic extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('tourism_goods_exotic_model', 'tourism_goods_exotic');
        $this->load->model('region_model', 'region');
    }

    public function grid($pg = 1)
    {
        $page_num = 20;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('tourismgoods/grid') . $this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('tourismgoodsexotic/grid');
        $config['total_rows'] = $this->tourism_goods_exotic->total($this->input->get());
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_list'] = $this->pagination->create_links();
        $data['page_list'] = $this->tourism_goods_exotic->page_list($page_num, $num, $this->input->get());
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $this->load->view('tourismgoodsexotic/grid', $data);
    }

    public function add()
    {
        //获取地区信息
        $data['regions'] = $this->region->children_of(1);
        $this->load->view('tourismgoodsexotic/add', $data);
    }

    public function addPost()
    {
        $postData = $this->input->post();
        $this->db->trans_start();
        $result = $this->tourism_goods_exotic->insertTourismGoodsExotic($postData);
        $this->db->trans_complete();
        
        if ($this->db->trans_status() === TRUE) {
            $this->success('tourismgoodsexotic/grid', '', '添加成功！');
        } else {
            $this->error('tourismgoodsexotic/grid', '', '添加失败！');
        }
    }

    public function edit($id)
    {
        $exotic = $this->tourism_goods_exotic->findById($id);
        if ($exotic->num_rows() <= 0) {
            $this->error('tourismgoodsexotic/grid', '', '内部错误！');
        }
        $data['regions'] = $this->region->children_of(1);
        $data['exotic'] = $exotic->row();
        $this->load->view('tourismgoodsexotic/edit',$data);
    }
    
    public function editPost()
    {
        $postData = $this->input->post();
        $this->db->trans_start();
        $result = $this->tourism_goods_exotic->updateTourismGoodsExotic($postData);
        $this->db->trans_complete();
    
        if ($this->db->trans_status() === TRUE) {
            $this->success('tourismgoodsexotic/grid', '', '修改成功！');
        } else {
            $this->error('tourismgoodsexotic/edit', $this->input->post('id'), '修改失败！');
        }
    }
}